<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Doraemon\model\Category as mCategory;
use Biz\service\Sku as sSku;
use Biz\service\search\Goods;
use Biz\service\esSearch\Goods as esGoods;

// 查询goods表数据 每次查询500条，根据这500条数据，查询solr，之后将solr的数据同步

$categoryList = mCategory::select('*');
$categoryList = array_column($categoryList, null, 'id');
$goodsList = mGoodsBasicInfo::select('*', ['status[!]' => 9]);

$field = ['id', 'is_alarm', 'add_cart_num', 'counter_rela_photos','material','style','tags_content','activities','heat_tag','heat_degree'];


$j = 0;
$s = 0;
for ($i=500;$i<=13000;$i=$i+500) {
    $goodsInfo = array_slice($goodsList, $j, 500);
    $goodsIds = array_column($goodsInfo, 'goods_id');
    $skuBasicInfos = sSku::SkuInfos($goodsIds, sSku::FIELDS_GROUP_BASE_WITH_STOCK);
    $solrList = Goods::getList(['id' => $goodsIds], $field, 1, 500);
    $solrList = $solrList['rows'];

    foreach ($goodsInfo as $data) {

        $info = [
            'second_category_id' => $data['second_category_id'],
            'third_category_id' => $data['third_category_id'],
            'four_category_id' => $data['four_category_id'],
            'second_category_name' => isset($categoryList[$data['second_category_id']]['cate_name']) ? $categoryList[$data['second_category_id']]['cate_name'] : '',
            'third_category_name' => isset($categoryList[$data['third_category_id']]['cate_name']) ?$categoryList[$data['third_category_id']]['cate_name']: '',
            'four_category_name' => isset($categoryList[$data['four_category_id']]['cate_name']) ?$categoryList[$data['four_category_id']]['cate_name']: '',
            'heat_tag' => isset($solrList[$data['goods_id']]['heat_tag']) && $solrList[$data['goods_id']]['heat_tag'] ? $solrList[$data['goods_id']]['heat_tag'] : '',
            'heat_degree' => isset($solrList[$data['goods_id']]['heat_degree']) ? $solrList[$data['goods_id']]['heat_degree'] : ''
        ];
        $data = array_merge(['id' => $data['goods_id']], $info);

        $res = esGoods::saveByGoodsId($data);
        if ($res['code'] != 1) {
            var_dump($res);
        }
        echo $data['goods_id'];
        echo PHP_EOL;
        $s++;
    }


//    var_dump(end($goodsList));die; 5726
//    var_dump($j,500);
    $j = $i;

}
echo $s;

die;



$j = 0;
$s = 0;
for ($i=500;$i<=5500;$i=$i+500) {
    $goodsInfo = array_slice($goodsList, $j, 500);
    $goodsIds = array_column($goodsInfo, 'goods_id');
    $skuBasicInfos = sSku::SkuInfos($goodsIds, sSku::FIELDS_GROUP_BASE_WITH_STOCK);
    $solrList = Goods::getList(['id' => $goodsIds], $field, 1, 500);
    $solrList = $solrList['rows'];

    foreach ($goodsInfo as $data) {

        $info = [
            'category_name' => isset($categoryList[$data['category_id']]['cate_name']) ? $categoryList[$data['category_id']]['cate_name'] : '',
            'second_category_name' => isset($categoryList[$data['second_category_id']]['cate_name']) ? $categoryList[$data['second_category_id']]['cate_name'] : '',
            'third_category_name' => isset($categoryList[$data['third_category_id']]['cate_name']) ?$categoryList[$data['third_category_id']]['cate_name']: '',
            'four_category_name' => isset($categoryList[$data['four_category_id']]['cate_name']) ?$categoryList[$data['four_category_id']]['cate_name']: '',
            'last_category_name' => isset($categoryList[$data['last_category_id']]['cate_name'])?$categoryList[$data['last_category_id']]['cate_name']: '',
            'min_price_has_stock' => $skuBasicInfos['min_price']??$data['min_price'],
            'max_price_has_stock' => $skuBasicInfos['max_price']??$data['max_price'],
            'is_alarm' => isset($solrList[$data['goods_id']]['is_alarm']) ? $solrList[$data['goods_id']]['is_alarm'] : 0,
            'app_status' => mGoodsBasicInfo::STATUS_APP_MAP[$data['status']],
            'add_cart_num' => 0,
            'counter_rela_photos' => isset($solrList[$data['goods_id']]['counter_rela_photos']) ? $solrList[$data['goods_id']]['counter_rela_photos'] : 0,
            'material' => isset($solrList[$data['goods_id']]['material']) ? $solrList[$data['goods_id']]['material']: 0,
            'style' => isset($solrList[$data['goods_id']]['style']) ? $solrList[$data['goods_id']]['style'] : 0,
            'tags_content' => isset($solrList[$data['goods_id']]['tags_content']) ? $solrList[$data['goods_id']]['tags_content'] : 0,
            'activity' =>isset( $solrList[$data['goods_id']]['activities']) ?  $solrList[$data['goods_id']]['activities'] : '',
            'heat_tag' => isset($solrList[$data['goods_id']]['heat_tag']) ? $solrList[$data['goods_id']]['heat_tag'] : '',
            'heat_degree' => isset($solrList[$data['goods_id']]['heat_degree']) ? $solrList[$data['goods_id']]['heat_degree'] : ''
        ];
        $data = array_merge(['id' => $data['goods_id']], $data, $info);

        $res = esGoods::saveByGoodsId($data);
        if ($res['code'] != 1) {
            var_dump($res);
        }
        echo $data['goods_id'];
        echo PHP_EOL;
        $s++;
    }


//    var_dump(end($goodsList));die; 5726
//    var_dump($j,500);
    $j = $i;

}
echo $s;
